﻿package com.gengine.resource.encryption
{
    import com.gengine.gc.*;
    import flash.utils.*;

    public class EncryptUtils extends Object
    {
        public static const JPG:String = "jpg";
        public static const SWF:String = "swf";
        public static const PNG:String = "png";
        public static const ATF:String = "atf";
        public static const DDS:String = "dds";
        public static const TGA:String = "tga";
        public static const NULL:String = "null";
        public static const MEGER:String = "meger";
        public static const extensionName:String = "abc";
        public static const normalJpgValue:int = -520103681;
        public static const normalJpgValue2:int = -503326465;
        public static const normalPngValue:int = 1196314761;
        public static const normalDDSValue:int = 542327876;
        public static const normalTGAValue:int = 131072;
        private static const baseNum:int = 10000;

        public function EncryptUtils()
        {
            return;
        }// end function

        public static function getExtensionByValue(param1:int) : String
        {
            if (param1 < baseNum)
            {
                if (param1 == normalJpgValue || param1 == normalJpgValue2)
                {
                    return JPG;
                }
                return NULL;
            }
            else
            {
                if (param1 < 2 * baseNum)
                {
                    return EncryptUtils.extensionName;
                }
                if (param1 < 3 * baseNum)
                {
                    return JPG;
                }
                if (param1 < 4 * baseNum || param1 == normalPngValue)
                {
                    return PNG;
                }
                if (param1 < 5 * baseNum || checkIsSwf(param1))
                {
                    return SWF;
                }
                if (param1 < 6 * baseNum || param1 == normalDDSValue)
                {
                    return DDS;
                }
                if (param1 < 7 * baseNum || param1 == normalTGAValue)
                {
                    return TGA;
                }
                if (param1 < 8 * baseNum || isATF(param1))
                {
                    return ATF;
                }
                if (param1 < 9 * baseNum)
                {
                    return MEGER;
                }
                return NULL;
            }
        }// end function

        public static function getContentExtension() : String
        {
            return null;
        }// end function

        private static function checkIsSwf(param1:int) : Boolean
        {
            var _loc_2:* = param1 & 255;
            var _loc_3:* = param1 & 65280;
            var _loc_4:* = param1 & 16711680;
            if (_loc_3 == 22272 && (_loc_2 == 70 || _loc_2 == 67) && _loc_4 == 5439488)
            {
                return true;
            }
            return false;
        }// end function

        private static function isATF(param1:int) : Boolean
        {
            var _loc_2:* = param1 & 16777215;
            if (_loc_2 == 4609089)
            {
                return true;
            }
            return false;
        }// end function

        public static function encodeAndMegerStream(param1:ByteArray, param2:ByteArray) : ByteArray
        {
            var _loc_3:* = new GByteArray();
            _loc_3.endian = Endian.LITTLE_ENDIAN;
            _loc_3.writeInt(getTypeValueByExtension(EncryptUtils.extensionName));
            _loc_3.writeInt(getTypeValueByExtension(EncryptUtils.MEGER));
            _loc_3.writeUnsignedInt(param1.bytesAvailable);
            _loc_3.writeBytes(param1);
            _loc_3.writeUnsignedInt(param2.bytesAvailable);
            _loc_3.writeBytes(param2);
            return _loc_3;
        }// end function

        public static function encodeStream(param1:EncryptionInfo) : ByteArray
        {
            var _loc_2:* = new GByteArray();
            _loc_2.endian = Endian.LITTLE_ENDIAN;
            if (param1.encryptType == null)
            {
                _loc_2.writeInt(getTypeValueByExtension(EncryptUtils.extensionName));
            }
            else
            {
                _loc_2.writeInt(getTypeValueByExtension(param1.encryptType));
            }
            _loc_2.writeInt(getTypeValueByExtension(param1.contentType));
            _loc_2.writeBytes(param1.contentBytes);
            return _loc_2;
        }// end function

        public static function decodeStream(param1:ByteArray) : EncryptionInfo
        {
            var _loc_2:ByteArray = null;
            var _loc_3:int = 0;
            var _loc_4:String = null;
            var _loc_5:String = null;
            var _loc_7:ByteArray = null;
            var _loc_8:uint = 0;
            var _loc_9:ByteArray = null;
            var _loc_10:uint = 0;
            var _loc_11:ByteArray = null;
            param1.endian = Endian.LITTLE_ENDIAN;
            param1.position = 0;
            _loc_3 = param1.readInt();
            var _loc_6:* = checkIsNormalFile(_loc_3);
            if (_loc_6)
            {
                _loc_5 = getExtensionByValue(_loc_3);
                _loc_4 = null;
                _loc_2 = param1;
            }
            else if (_loc_3 < 8 * baseNum && _loc_3 >= baseNum)
            {
                _loc_4 = getExtensionByValue(_loc_3);
                _loc_5 = getExtensionByValue(param1.readInt());
                if (_loc_5 == MEGER)
                {
                    _loc_8 = param1.readUnsignedInt();
                    _loc_9 = new GByteArray();
                    _loc_9.endian = Endian.LITTLE_ENDIAN;
                    param1.readBytes(_loc_9, 0, _loc_8);
                    _loc_10 = param1.readUnsignedInt();
                    _loc_11 = new GByteArray();
                    _loc_11.endian = Endian.LITTLE_ENDIAN;
                    param1.readBytes(_loc_11, 0, _loc_10);
                    _loc_2 = _loc_9;
                    _loc_7 = _loc_11;
                    param1.clear();
                }
                else
                {
                    _loc_2 = new GByteArray();
                    _loc_2.endian = Endian.LITTLE_ENDIAN;
                    param1.readBytes(_loc_2, 0, param1.bytesAvailable);
                    param1.clear();
                }
            }
            else
            {
                throw new Error("不合法类型文件,如是" + EncryptUtils.extensionName + "文件，请重新加密！");
            }
            return new EncryptionInfo(_loc_4, _loc_2, _loc_5, _loc_7);
        }// end function

        public static function checkIsNormalFile(param1:int) : Boolean
        {
            if (param1 == normalJpgValue || param1 == normalJpgValue2 || param1 == normalPngValue)
            {
                return true;
            }
            if (param1 == normalDDSValue || param1 == normalTGAValue || isATF(param1) || checkIsSwf(param1))
            {
                return true;
            }
            return false;
        }// end function

        public static function getTypeValueByExtension(param1:String) : int
        {
            param1 = param1.toLowerCase();
            var _loc_2:* = Math.random() * baseNum;
            if (param1 == EncryptUtils.extensionName)
            {
                return _loc_2 + baseNum;
            }
            if (param1 == JPG)
            {
                return _loc_2 + baseNum * 2;
            }
            if (param1 == PNG)
            {
                return _loc_2 + baseNum * 3;
            }
            if (param1 == SWF)
            {
                return _loc_2 + baseNum * 4;
            }
            if (param1 == DDS)
            {
                return _loc_2 + baseNum * 5;
            }
            if (param1 == TGA)
            {
                return _loc_2 + baseNum * 6;
            }
            if (param1 == ATF)
            {
                return _loc_2 + baseNum * 7;
            }
            if (param1 == MEGER)
            {
                return _loc_2 + baseNum * 8;
            }
            throw new Error("扩展名称有误！");
        }// end function

    }
}
